﻿using System;
using System.Data;
using System.Collections;
using CE.Database;

/// <summary>
/// Summary description for FleetCarPolicyDB
/// </summary>
namespace CE.CarInsurance
{
    public class FleetCarPolicyDB
    {
        public Int64 Insert(FleetCarPolicy fleetcarpolicy)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocFleetCarPolicyInsertUpdateSingleItem";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@FleetCarPolicyId", fleetcarpolicy.FleetCarPolicyId);
            _sPara.Add("@FleetCarId", fleetcarpolicy.FleetCarId);
            _sPara.Add("@CompanyId", fleetcarpolicy.CompanyId);
            _sPara.Add("@InsuranceType", fleetcarpolicy.InsuranceType);
            _sPara.Add("@GarageType", fleetcarpolicy.GarageType);
            _sPara.Add("@VehicleType", fleetcarpolicy.VehicleType);
            _sPara.Add("@SumInsured", fleetcarpolicy.SumInsured);
            _sPara.Add("@ProvinceRegistration", fleetcarpolicy.ProvinceRegistration);
            _sPara.Add("@UseOfVehicle", fleetcarpolicy.UseOfVehicle);
            _sPara.Add("@IsRedPlate", fleetcarpolicy.IsRedPlate);
            _sPara.Add("@IsAddAccMod", fleetcarpolicy.IsAddAccMod);
            _sPara.Add("@AddAccModAmount", fleetcarpolicy.AddAccModAmount);
            _sPara.Add("@CoverageCommenceDate", fleetcarpolicy.CoverageCommenceDate);
            _sPara.Add("@NCDPercent", fleetcarpolicy.NCDPercent);
            _sPara.Add("@IsClaimLastYear", fleetcarpolicy.IsClaimLastYear);
            _sPara.Add("@IsAddDriver", fleetcarpolicy.IsAddDriver);
            _sPara.Add("@NoOfDriver", fleetcarpolicy.NoOfDriver);
            _sPara.Add("@Deductible", fleetcarpolicy.Deductible);
            _sPara.Add("@IsCTPL", fleetcarpolicy.IsCTPL);
            _sPara.Add("@CurrentInsurer", fleetcarpolicy.CurrentInsurer);
            _sPara.Add("@CurrentStep", fleetcarpolicy.CurrentStep);
            _sPara.Add("@AgentId", fleetcarpolicy.AgentId);
            _sPara.Add("@RefCEUserId", fleetcarpolicy.RefCEUserId);
            _sPara.Add("@Status", fleetcarpolicy.Status);
            _sPara.Add("@NInfo1", fleetcarpolicy.NInfo1);
            _sPara.Add("@Info1", fleetcarpolicy.Info1);
            _sPara.Add("@Info2", fleetcarpolicy.Info2);
            _sPara.Add("@Info3", fleetcarpolicy.Info3);
            _sPara.Add("@Info4", fleetcarpolicy.Info4);
            _sPara.Add("@Info5", fleetcarpolicy.Info5);
            _sPara.Add("@Info6", fleetcarpolicy.Info6);
            _sPara.Add("@Info7", fleetcarpolicy.Info7);
            _sPara.Add("@Info8", fleetcarpolicy.Info8);
            _sPara.Add("@Info9", fleetcarpolicy.Info9);
            _sPara.Add("@Info10", fleetcarpolicy.Info10);
            _sPara.Add("@Info11", fleetcarpolicy.Info11);
            _sPara.Add("@Info12", fleetcarpolicy.Info12);
            _sPara.Add("@Info13", fleetcarpolicy.Info13);
            _sPara.Add("@Info14", fleetcarpolicy.Info14);
            _sPara.Add("@Info15", fleetcarpolicy.Info15);
            _sPara.Add("@Info16", fleetcarpolicy.Info16);
            _sPara.Add("@Info17", fleetcarpolicy.Info17);
            _sPara.Add("@Info18", fleetcarpolicy.Info18);
            _sPara.Add("@Info19", fleetcarpolicy.Info19);
            _sPara.Add("@Info20", fleetcarpolicy.Info20);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public FleetCarPolicy GetCarPolicy(Int64 FleetCarId, Int64 CompanyId, Int64 FleetPolicyId)
        {
            FleetCarPolicy _fleetcarpolicy = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocFleetCarPolicySelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@FleetCarId", FleetCarId);
            _spara.Add("@FleetCarPolicyId", FleetPolicyId);
            _spara.Add("@CompanyId", CompanyId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _fleetcarpolicy = new FleetCarPolicy();
                    _fleetcarpolicy = FillDataRecord(_dt);
                }
            }
            return _fleetcarpolicy;
        }

        public FleetCarPolicy GetCarPolicy(Int64 FleetCarId, Int64 CompanyId)
        {
            FleetCarPolicy _fleetcarpolicy = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocFleetCarPolicySelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@FleetCarId", FleetCarId);
            _spara.Add("@CompanyId", CompanyId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _fleetcarpolicy = new FleetCarPolicy();
                    _fleetcarpolicy = FillDataRecord(_dt);
                }
            }
            return _fleetcarpolicy;
        }

        private FleetCarPolicy FillDataRecord(DataTable myDataTable)
        {
            FleetCarPolicy myPolicy = new FleetCarPolicy();
            myPolicy.FleetCarPolicyId = Convert.ToInt64(myDataTable.Rows[0]["FleetCarPolicyId"].ToString());
            myPolicy.FleetCarId = Convert.ToInt32(myDataTable.Rows[0]["FleetCarId"].ToString());
            myPolicy.CompanyId = Convert.ToInt64(myDataTable.Rows[0]["CompanyId"].ToString());
            myPolicy.InsuranceType = Convert.ToInt32(myDataTable.Rows[0]["InsuranceType"].ToString());
            myPolicy.GarageType = Convert.ToInt32(myDataTable.Rows[0]["GarageType"].ToString());
            myPolicy.VehicleType = Convert.ToInt32(myDataTable.Rows[0]["VehicleType"].ToString());
            myPolicy.SumInsured = Convert.ToInt32(myDataTable.Rows[0]["SumInsured"].ToString());
            myPolicy.ProvinceRegistration = myDataTable.Rows[0]["ProvinceRegistration"].ToString();
            myPolicy.UseOfVehicle = myDataTable.Rows[0]["UseOfVehicle"].ToString();
            myPolicy.IsRedPlate = Convert.ToBoolean(myDataTable.Rows[0]["IsRedPlate"].ToString());
            myPolicy.IsAddAccMod = Convert.ToBoolean(myDataTable.Rows[0]["IsAddAccMod"].ToString());
            myPolicy.AddAccModAmount = Convert.ToInt32(myDataTable.Rows[0]["AddAccModAmount"].ToString());
            myPolicy.CoverageCommenceDate = Convert.ToDateTime(myDataTable.Rows[0]["CoverageCommenceDate"].ToString());
            myPolicy.NCDPercent = Convert.ToInt32(myDataTable.Rows[0]["NCDPercent"].ToString());
            myPolicy.IsClaimLastYear = Convert.ToBoolean(myDataTable.Rows[0]["IsClaimLastYear"].ToString());
            myPolicy.NoOfDriver = Convert.ToInt16(myDataTable.Rows[0]["NoOfDriver"].ToString());
            myPolicy.Deductible = Convert.ToInt16(myDataTable.Rows[0]["Deductible"].ToString());
            myPolicy.AgentId = Convert.ToInt64(myDataTable.Rows[0]["AgentId"].ToString());
            myPolicy.RefCEUserId = Convert.ToInt64(myDataTable.Rows[0]["RefCEUserId"].ToString());
            myPolicy.IsAddDriver = Convert.ToBoolean(myDataTable.Rows[0]["IsAddDriver"].ToString());
            myPolicy.IsCTPL = Convert.ToBoolean(myDataTable.Rows[0]["IsCTPL"].ToString());
            myPolicy.CurrentInsurer = myDataTable.Rows[0]["CurrentInsurer"].ToString();
            myPolicy.Status = Convert.ToBoolean(myDataTable.Rows[0]["Status"].ToString());
            myPolicy.NInfo1 = Convert.ToInt64(myDataTable.Rows[0]["NInfo1"].ToString());

            if (myDataTable.Rows[0]["Info6"] != DBNull.Value)
            {
                myPolicy.Info6 = myDataTable.Rows[0]["Info6"].ToString();
            }

            if (myDataTable.Rows[0]["Info7"] != DBNull.Value)
            {
                myPolicy.Info7 = myDataTable.Rows[0]["Info7"].ToString();
            }

            if (myDataTable.Rows[0]["Info8"] != DBNull.Value)
            {
                myPolicy.Info8 = myDataTable.Rows[0]["Info8"].ToString();
            }
            return myPolicy;
        }

    }
}